"use strict";
var clickFunction = function (keyEvent) {
    keyEvent = keyEvent ? keyEvent : window.event;
    keyEvent.which = keyEvent.which ? keyEvent.which : keyEvent.keyCode;
    keycontrol(keyEvent);
};
//window.document.onkeypress = clickFunction;
window.document.onkeydown = clickFunction;

//按键操作
function keycontrol (obj) {
    if (obj.which === 0x0026) {
        /* 上 */
        keyopt.moveBtn("go_top");
    } else if (obj.which === 0x0028) {
        /* 下 */
        keyopt.moveBtn("go_bottom");
    } else if (obj.which === 0x0025) {
        /* 左 */
        keyopt.moveBtn("go_left");
    } else if (obj.which === 0x0027) {
        /* 右 */
        keyopt.moveBtn("go_right");
    } else if (obj.which === 0x000d) {
        /* 确定 */
        keyopt.marksure();
    } else if (
        (obj.which <= 0x0039 && obj.which >= 0x0030) ||
        obj.which === 0x118 ||
        obj.which === 280
    ) {
        //数字键输入
        keyopt.input(obj.which);
    } else if (obj.which === 113 || obj.which === 0x0008) {
            //返回键
            history.go(-1);
    } else if (obj.which === 0x0447) {//音量＋键
    } else if (obj.which === 0x0448) {//音量－键
    }
}

//操作类型
var keyopt = {
    options: {
        focusElm: "t1"
    },
    moveBtn: function (toDirection) {
        // 移动之前的id
        var oldBtnId = this.options.focusElm;
        // 将移动之前的元素json信息解析成json对象
        var old_obj_json = eval(oldBtnId + "_json");
        // 获取移动目的地的id信息
        var goElm = old_obj_json[toDirection];
        if (goElm === "") {
            goElm = oldBtnId;
        }
        var new_btn = document.getElementById(goElm);
        if (new_btn) {
            var onfocusFuc = eval(goElm + "_onfocus"); //之后ID的blur方法
            var onblurFuc = eval(oldBtnId + "_onblur"); //之前ID的focus方法
            onblurFuc(toDirection, oldBtnId); //go_buttom,top1
            this.options.focusElm = goElm;
            onfocusFuc(toDirection, goElm); //go_buttom,top2
        }
    },
    marksure: function () {
        var dom = document.getElementById(this.options.focusElm);
        if (dom) {
            var onsubmitFuc = eval(this.options.focusElm + "_onsubmit");
            if (dom.dataset.url) {
                location.href = dom.dataset.url;
            }
            onsubmitFuc("", this.options.focusElm);
        }
    },
    input: function (code) {
        var onkeypressFuc = eval(this.options.focusElm + "_onkeypress");
        onkeypressFuc(code);
    },
    moveToElm: function (id) {
        var to_elm = document.getElementById(id);
        if (to_elm) {
            var onfocusFuc = eval(id + "_onfocus");
            var onblurFuc = eval(this.options.focusElm + "_onblur");
            onblurFuc('',this.options.focusElm);
            this.options.focusElm = id;
            onfocusFuc('',this.options.focusElm);
        }
    }
};